//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.qst.crop.controller;

import com.github.pagehelper.PageInfo;
import com.qst.crop.common.Result;
import com.qst.crop.entity.Order;
import com.qst.crop.entity.PurchaseDetail;
import com.qst.crop.entity.SellPurchase;
import com.qst.crop.model.MyPurchase;
import com.qst.crop.service.OrderService;
import com.qst.crop.service.PurchaseDetailService;
import com.qst.crop.service.PurchaseService;
import com.qst.crop.service.SellPurchaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

@Api(
        tags = {"订单模块接口"}
)
@RestController
@RequestMapping({"/order"})
@CrossOrigin
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private PurchaseService purchaseService;
    @Autowired
    private PurchaseDetailService purchaseDetailService;
    @Autowired
    private SellPurchaseService sellPurchaseService;

    public OrderController() {
    }

    @ApiOperation("查询所有商品")
    @GetMapping({"/All/{pageNum}"})
    public Result<PageInfo> selectAll(@PathVariable("pageNum") Integer pageNum) {
        PageInfo<Order> orders = this.orderService.selectAll(pageNum);
        return new Result(true, 20000, "查询成功", orders);
    }

    @ApiOperation("分页查询所有货源(商品)商品")
    @GetMapping({"/goods/{pageNum}"})
    public Result<PageInfo> selectAllGoods(@PathVariable("pageNum") Integer pageNum) {
        PageInfo<Order> orders = this.orderService.selectAllGoods(pageNum);
        return new Result(true, 20000, "查询成功", orders);
    }

    @ApiOperation("分页查询所有需求")
    @GetMapping({"/needs/{pageNum}"})
    public Result<PageInfo> selectAllNeeds(@PathVariable("pageNum") Integer pageNum) {
        PageInfo<Order> orders = this.orderService.selectAllNeeds(pageNum);
        return new Result(true, 20000, "查询成功", orders);
    }

    @ApiOperation("添加商品")
    @PostMapping
    public Result<String> add(@RequestBody @Valid Order order, BindingResult bindingResult) {
        if (!bindingResult.hasErrors()) {
            UserDetails principal = (UserDetails)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            String name = principal.getUsername();
            order.setOwnName(name);
            order.setCreateTime(new Date());
            order.setUpdateTime(new Date());
            this.orderService.add(order);
            return new Result(true, 20000, "添加成功", (Object)null);
        } else {
            StringBuffer stringBuffer = new StringBuffer();
            List<ObjectError> allErrors = bindingResult.getAllErrors();
            Iterator var5 = allErrors.iterator();

            while(var5.hasNext()) {
                ObjectError objectError = (ObjectError)var5.next();
                stringBuffer.append(objectError.getDefaultMessage()).append("; ");
            }

            String s = stringBuffer.toString();
            System.out.println(s);
            return new Result(false, 20001, "添加失败", s);
        }
    }

    @ApiOperation("根据id修改商品")
    @PutMapping({"/{id}"})
    public Result<String> update(@Validated @RequestBody Order order, BindingResult bindingResult, @PathVariable Integer id) {
        if (!bindingResult.hasErrors()) {
            order.setUpdateTime(new Date());
            order.setOrderId(id);
            this.orderService.update(order);
            return new Result(true, 20000, "修改成功", (Object)null);
        } else {
            StringBuffer stringBuffer = new StringBuffer();
            List<ObjectError> allErrors = bindingResult.getAllErrors();
            Iterator var6 = allErrors.iterator();

            while(var6.hasNext()) {
                ObjectError objectError = (ObjectError)var6.next();
                stringBuffer.append(objectError.getDefaultMessage()).append("; ");
            }

            String s = stringBuffer.toString();
            System.out.println(s);
            return new Result(false, 20001, "修改失败", s);
        }
    }

    @ApiOperation("根据id删除商品")
    @DeleteMapping({"/{id}"})
    public Result delete(@PathVariable("id") Integer id) {
        this.orderService.delete(id);
        return new Result(true, 20000, "删除成功");
    }

    @ApiOperation("根据用户名+订单类型查询商品")
    @GetMapping({"/search/{type}/{pageNum}"})
    public Result<PageInfo> selectByType(@PathVariable("type") String type, @PathVariable("pageNum") Integer pageNum) {
        PageInfo<Order> orders = this.orderService.selectByType(type, pageNum);
        return new Result(true, 20000, "查询成功", orders);
    }

    @ApiOperation("根据id查询商品")
    @GetMapping({"/selectById/{id}"})
    public Result<Order> selectById(@PathVariable("id") Integer id) {
        Order order = this.orderService.selectById(id);
        return new Result(true, 20000, "查询成功", order);
    }

    @ApiOperation("根据登录用户查询我买的订单")
    @GetMapping({"/selectBuys"})
    public Result<List<MyPurchase>> selectBuys() {
        List<MyPurchase> purchase = this.purchaseService.selectByPurchaseType();
        return new Result(true, 20000, "查询成功", purchase);
    }

    @ApiOperation("根据登录用户查询我买的订单详情")
    @GetMapping({"/selectBuysDetail/{id}"})
    public Result<List<PurchaseDetail>> selectBuysDetail(@PathVariable("id") Integer purchaseId) {
        List<PurchaseDetail> purchaseDetail = this.purchaseDetailService.selectByPurchaseId(purchaseId);
        return new Result(true, 20000, "查询成功", purchaseDetail);
    }

    @ApiOperation("根据登录用户查询我卖出的订单")
    @GetMapping({"/selectSells"})
    public Result<List<SellPurchase>> selectSells() {
        List<SellPurchase> purchase = this.sellPurchaseService.selectByName();
        return new Result(true, 20000, "查询成功", purchase);
    }

    @ApiOperation("分页条件搜索商品（货源）订单")
    @GetMapping({"/searchGoodsByKeys/{keys}/{pageNum}"})
    public Result<PageInfo> searchGoodsByKeys(@PathVariable("keys") String keys, @PathVariable("pageNum") Integer pageNum) {
        PageInfo<Order> orders = this.orderService.selectGoodsByKeys(keys, pageNum, (String)null);
        return new Result(true, 20000, "查询成功", orders);
    }

    @ApiOperation("分页条件搜索需求商品")
    @GetMapping({"/searchNeedsByKeys/{keys}/{pageNum}"})
    public Result<PageInfo> searchNeedsByKeys(@PathVariable("keys") String keys, @PathVariable("pageNum") Integer pageNum) {
        PageInfo<Order> orders = this.orderService.selectNeedsByKeys(keys, pageNum, (String)null);
        return new Result(true, 20000, "查询成功", orders);
    }

    @ApiOperation("分页条件搜索所有商品")
    @GetMapping({"/searchAllByKeys/{keys}/{pageNum}"})
    public Result<PageInfo> searchAllByKeys(@PathVariable("keys") String keys, @PathVariable("pageNum") Integer pageNum) {
        PageInfo<Order> orders = this.orderService.selectAllByKeys(keys, pageNum);
        return new Result(true, 20000, "查询成功", orders);
    }

    @ApiOperation("分页条件搜索需求商品")
    @GetMapping({"/searchMyNeedsByKeys/{keys}/{pageNum}"})
    public Result<PageInfo> searchMyNeedsByKeys(@PathVariable("keys") String keys, @PathVariable("pageNum") Integer pageNum) {
        UserDetails principal = (UserDetails)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String name = principal.getUsername();
        PageInfo<Order> orders = this.orderService.selectNeedsByKeys(keys, pageNum, name);
        return new Result(true, 20000, "查询成功", orders);
    }

    @ApiOperation("分页条件搜索商品（货源）商品")
    @GetMapping({"/searchMyGoodsByKeys/{keys}/{pageNum}"})
    public Result<PageInfo> searchMyGoodsByKeys(@PathVariable("keys") String keys, @PathVariable("pageNum") Integer pageNum) {
        UserDetails principal = (UserDetails)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String name = principal.getUsername();
        PageInfo<Order> orders = this.orderService.selectGoodsByKeys(keys, pageNum, name);
        return new Result(true, 20000, "查询成功", orders);
    }

    @ApiOperation("商品下架")
    @PutMapping({"/takeDownOrder/{orderId}"})
    public Result takeDownOrder(@PathVariable("orderId") String orderId) {
        this.orderService.takeDown(orderId);
        return new Result(true, 20000, "下架成功");
    }

    @ApiOperation("商品上架")
    @PutMapping({"/takeUpOrder/{orderId}"})
    public Result takeUpOrder(@PathVariable("orderId") String orderId) {
        this.orderService.takeUp(orderId);
        return new Result(true, 20000, "上架成功");
    }
}
